
package com.networklib.http.interceptor;

import com.networklib.http.impl.ProgressRequestBodyImpl;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class UploadProgressInterceptor implements Interceptor {
    private ProgressRequestBodyImpl.ProgressListener progressListener = new ProgressRequestBodyImpl.ProgressListener() {
        public void update(long bytesRead, long contentLength, boolean done) {
            System.out.println(bytesRead);
            System.out.println(contentLength);
            System.out.println(done);
            System.out.format("%d%% done\n", new Object[]{Long.valueOf(100L * bytesRead / contentLength)});
        }
    };

    public UploadProgressInterceptor(ProgressRequestBodyImpl.ProgressListener progressListener) {
        if(progressListener != null) {
            this.progressListener = progressListener;
        }

    }

    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        request = request.newBuilder().post(new ProgressRequestBodyImpl(request.body(), this.progressListener)).build();
        return chain.proceed(request);
    }
}
